package com.guoxin.thread.concurrent.chapter9;

public class ProducerConsumerVersion1 {

    private int i = 1;

    private final Object LOCK = new Object();

    public void producer() {
        synchronized (LOCK) {
            System.out.println("生产数据>>" + i++);
        }
    }

    public void consumer() {
        synchronized (LOCK) {
            System.out.println("消费数据>>" + i);
        }
    }

    public static void main(String[] args) {
        ProducerConsumerVersion1 pc = new ProducerConsumerVersion1();

        new Thread("P") {
            @Override
            public void run() {
                while (true) {
                    pc.producer();
                }
            }
        }.start();

        new Thread("C") {
            @Override
            public void run() {
                while (true) {
                    pc.consumer();
                }
            }
        }.start();
    }
}
